home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / gnu / bash / bash_110 / mint / bash110s.zoo / bash-1.10 / builtins / mkbuiltins.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-09-06  |  27.0 KB  |  1,085 lines

  1. /* mkbuiltins.c - Create builtins.c, builtext.h, and builtdoc.c from
  2.    a single source file called builtins.def. */
  3.  
  4. /* Copyright (C) 1987, 1989, 1991 Free Software Foundation, Inc.
  5.  
  6. This file is part of GNU Bash, the Bourne Again SHell.
  7.  
  8. Bash is free software; you can redistribute it and/or modify it under
  9. the terms of the GNU General Public License as published by the Free
  10. Software Foundation; either version 1, or (at your option) any later
  11. version.
  12.  
  13. Bash is distributed in the hope that it will be useful, but WITHOUT ANY
  14. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  15. FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  16. for more details.
  17.  
  18. You should have received a copy of the GNU General Public License along
  19. with Bash; see the file COPYING.  If not, write to the Free Software
  20. Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
  21.  
  22. #include <stdio.h>
  23. #include <sys/types.h>
  24. #include <sys/file.h>
  25. #include <sys/stat.h>
  26. #include "../filecntl.h"
  27.  
  28. #define DOCFILE "builtins.texi"
  29.  
  30. static char *xmalloc (), *xrealloc ();
  31. #define savestring(x) (char *)strcpy (xmalloc (1 + strlen (x)), (x))
  32. #define whitespace(c) (((c) == ' ') || ((c) == '\t'))
  33.  
  34. /* If this stream descriptor is non-zero, then write
  35.    texinfo documentation to it. */
  36. FILE *documentation_file = (FILE *)NULL;
  37.  
  38. /* Non-zero means to only produce documentation. */
  39. int only_documentation = 0;
  40.  
  41. /* Non-zero means to not do any productions. */
  42. int inhibit_production = 0;
  43.  
  44. /* The name of a directory to precede the filename when reporting
  45.    errors. */
  46. char *error_directory = (char *)NULL;
  47.  
  48. /* The name of the structure file. */
  49. char *struct_filename = (char *)NULL;
  50.  
  51. /* The name of the external declaration file. */
  52. char *extern_filename = (char *)NULL;
  53.  
  54. /* For each file mentioned on the command line, process it and
  55.    write the information to STRUCTFILE and EXTERNFILE, while
  56.    creating the production file if neccessary. */
  57. main (argc, argv)
  58.      int argc;
  59.      char **argv;
  60. {
  61.   int arg_index = 1;
  62.   FILE *structfile, *externfile;
  63.   char *documentation_filename, *temp_struct_filename;
  64.  
  65.   structfile = externfile = (FILE *)NULL;
  66.   documentation_filename = DOCFILE;
  67.  
  68.   while (arg_index < argc && argv[arg_index][0] == '-')
  69.     {
  70.       char *arg = argv[arg_index++];
  71.  
  72.       if (strcmp (arg, "-externfile") == 0)
  73.     extern_filename = argv[arg_index++];
  74.       else if (strcmp (arg, "-structfile") == 0)
  75.     struct_filename = argv[arg_index++];
  76.       else if (strcmp (arg, "-noproduction") == 0)
  77.     inhibit_production = 1;
  78.       else if (strcmp (arg, "-document") == 0)
  79.     documentation_file = fopen (documentation_filename, "w");
  80.       else if (strcmp (arg, "-D") == 0)
  81.     {
  82.       int len;
  83.  
  84.       if (error_directory)
  85.         free (error_directory);
  86.  
  87.       error_directory = (char *)xmalloc (2 + strlen (argv[arg_index]));
  88.       strcpy (error_directory, argv[arg_index]);
  89.       len = strlen (error_directory);
  90.  
  91.       if (len && error_directory[len - 1] != '/')
  92.         strcat (error_directory, "/");
  93.  
  94.       arg_index++;
  95.     }
  96.       else if (strcmp (arg, "-documentonly") == 0)
  97.     {
  98.       only_documentation = 1;
  99.       documentation_file = fopen (documentation_filename, "w");
  100.     }
  101.       else
  102.     {
  103.       fprintf (stderr, "%s: Unknown flag %s.\n", argv[0], arg);
  104.       exit (2);
  105.     }
  106.     }
  107.  
  108.   /* If there are no files to process, just quit now. */
  109.   if (arg_index == argc)
  110.     exit (0);
  111.  
  112.   if (!only_documentation)
  113.     {
  114.       /* Open the files. */
  115.       if (struct_filename)
  116.     {
  117.       temp_struct_filename = (char *)xmalloc (15);
  118.       sprintf (temp_struct_filename, "mk-%d", (int) getpid ());
  119.       structfile = fopen (temp_struct_filename, "w");
  120.  
  121.       if (!structfile)
  122.         file_error (struct_filename);
  123.     }
  124.  
  125.       if (extern_filename)
  126.     {
  127.       externfile = fopen (extern_filename, "w");
  128.  
  129.       if (!externfile)
  130.         file_error (extern_filename);
  131.     }
  132.  
  133.       /* Write out the headers. */
  134.       write_file_headers (structfile, externfile);
  135.     }
  136.  
  137.   if (documentation_file)
  138.     {
  139.       fprintf (documentation_file, "@c Table of builtins created with %s.\n",
  140.            argv[0]);
  141.       fprintf (documentation_file, "@ftable @asis\n");
  142.     }
  143.  
  144.   /* Process the .def files. */
  145.   while (arg_index < argc)
  146.     {
  147.       register char *arg;
  148.  
  149.       arg = argv[arg_index++];
  150.  
  151.       extract_info (arg, structfile, externfile);
  152.     }
  153.  
  154.   /* Close the files. */
  155.   if (!only_documentation)
  156.     {
  157.       /* Write the footers. */
  158.       write_file_footers (structfile, externfile);
  159.  
  160.       if (structfile)
  161.     {
  162.       fclose (structfile);
  163.       link (temp_struct_filename, struct_filename);
  164.       unlink (temp_struct_filename);
  165.     }
  166.  
  167.       if (externfile)
  168.     fclose (externfile);
  169.     }
  170.  
  171.   if (documentation_file)
  172.     {
  173.       fprintf (documentation_file, "@end ftable\n");
  174.       fclose (documentation_file);
  175.     }
  176.  
  177.   exit (0);
  178. }
  179.  
  180. /* **************************************************************** */
  181. /*                                    */
  182. /*          Array Functions and Manipulators            */
  183. /*                                    */
  184. /* **************************************************************** */
  185.  
  186. /* Here is a structure for manipulating arrays of data. */
  187. typedef struct {
  188.   int size;        /* Number of slots allocated to array. */
  189.   int index;        /* Current location in array. */
  190.   int width;        /* Size of each element. */
  191.   int growth_rate;    /* How fast to grow. */
  192.   char **array;        /* The array itself. */
  193. } ARRAY;
  194.  
  195. /* Make a new array, and return a pointer to it.  The array will
  196.    contain elements of size WIDTH, and is initialized to no elements. */
  197. ARRAY *
  198. array_create (width)
  199.      int width;
  200. {
  201.   ARRAY *array;
  202.  
  203.   array = (ARRAY *)xmalloc (sizeof (ARRAY));
  204.   array->size = 0;
  205.   array->index = 0;
  206.   array->width = width;
  207.  
  208.   /* Default to increasing size in units of 20. */
  209.   array->growth_rate = 20;
  210.  
  211.   array->array = (char **)NULL;
  212.  
  213.   return (array);
  214. }
  215.  
  216. /* Add ELEMENT to ARRAY, growing the array if neccessary. */
  217. array_add (element, array)
  218.      char *element;
  219.      ARRAY *array;
  220. {
  221.   if (array->index + 2 > array->size)
  222.     array->array = (char **)xrealloc
  223.       (array->array, (array->size += array->growth_rate) * array->width);
  224.  
  225. #if defined (HAVE_BCOPY)
  226.   bcopy (&element, &(array->array[array->index]), array->width);
  227.   array->index++;
  228.   bzero (&(array->array[array->index]), array->width);
  229. #else
  230.   array->array[array->index++] = element;
  231.   array->array[array->index] = (char *)NULL;
  232. #endif /* !HAVE_BCOPY */
  233. }
  234.  
  235. /* Free an allocated array and data pointer. */
  236. array_free (array)
  237.      ARRAY *array;
  238. {
  239.   if (array->array)
  240.     free (array->array);
  241.  
  242.   free (array);
  243. }
  244.  
  245. /* **************************************************************** */
  246. /*                                    */
  247. /*               Processing a DEF File                */
  248. /*                                    */
  249. /* **************************************************************** */
  250.  
  251. /* The definition of a function. */
  252. typedef int Function ();
  253.  
  254. /* Structure handles processor directives. */
  255. typedef struct {
  256.   char *directive;
  257.   Function *function;
  258. } HANDLER_ENTRY;
  259.  
  260. extern int
  261.   builtin_handler (), function_handler (), short_doc_handler (),
  262.   comment_handler (), depends_on_handler (), produces_handler (),
  263.   end_handler ();
  264.  
  265. HANDLER_ENTRY handlers[] = {
  266.   { "BUILTIN", builtin_handler },
  267.   { "FUNCTION", function_handler },
  268.   { "SHORT_DOC", short_doc_handler },
  269.   { "$", comment_handler },
  270.   { "COMMENT", comment_handler },
  271.   { "DEPENDS_ON", depends_on_handler },
  272.   { "PRODUCES", produces_handler },
  273.   { "END", end_handler },
  274.   { (char *)NULL, (Function *)NULL }
  275. };
  276.  
  277. /* Return the entry in the table of handlers for NAME. */
  278. HANDLER_ENTRY *
  279. find_directive (directive)
  280.      char *directive;
  281. {
  282.   register int i;
  283.  
  284.   for (i = 0; handlers[i].directive; i++)
  285.     if (strcmp (handlers[i].directive, directive) == 0)
  286.       return (&handlers[i]);
  287.  
  288.   return ((HANDLER_ENTRY *)NULL);
  289. }
  290.  
  291. /* Here is a structure defining a single BUILTIN. */
  292. typedef struct {
  293.   char *name;        /* The name of this builtin. */
  294.   char *function;    /* The name of the function to call. */
  295.   char *shortdoc;    /* The short documentation for this builtin. */
  296.   ARRAY *longdoc;    /* The long documentation for this builtin. */
  297.   ARRAY *dependencies;    /* Null terminated array of #define names. */
  298. } BUILTIN_DESC;
  299.  
  300. /* Here is a structure which defines a DEF file. */
  301. typedef struct {
  302.   char *filename;    /* The name of the input def file. */
  303.   ARRAY *lines;        /* The contents of the file. */
  304.   int line_number;    /* The current line number. */
  305.   char *production;    /* The name of the production file. */
  306.   FILE *output;        /* Open file stream for PRODUCTION. */
  307.   ARRAY *builtins;    /* Null terminated array of BUILTIN_DESC *. */
  308. } DEF_FILE;
  309.  
  310. /* Non-zero indicates that a $BUILTIN has been seen, but not
  311.    the corresponding $END. */
  312. static int building_builtin = 0;
  313.  
  314. /* Non-zero means to output cpp line and file information before
  315.    printing the current line to the production file. */
  316. int output_cpp_line_info = 0;
  317.  
  318. /* The main function of this program.  Read FILENAME and act on what is
  319.    found.  Lines not starting with a dollar sign are copied to the
  320.    $PRODUCES target, if one is present.  Lines starting with a dollar sign
  321.    are directives to this program, specifying the name of the builtin, the
  322.    function to call, the short documentation and the long documentation
  323.    strings.  FILENAME can contain multiple $BUILTINs, but only one $PRODUCES
  324.    target.  After the file has been processed, write out the names of
  325.    builtins found in each $BUILTIN.  Plain text found before the $PRODUCES
  326.    is ignored, as is "$$ comment text". */
  327. extract_info (filename, structfile, externfile)
  328.      char *filename;
  329.      FILE *structfile, *externfile;
  330. {
  331.   register int i;
  332.   DEF_FILE *defs;
  333.   ARRAY *lines;
  334.   struct stat finfo;
  335.   char *buffer, *line;
  336.   int fd;
  337.  
  338.   if (stat (filename, &finfo) == -1)
  339.     file_error (filename);
  340.  
  341.   fd = open (filename, O_RDONLY, 0666);
  342.  
  343.   if (fd == -1)
  344.     file_error (filename);
  345.  
  346.   buffer = xmalloc (1 + finfo.st_size);
  347.  
  348.   if (read (fd, buffer, finfo.st_size) != finfo.st_size)
  349.     file_error (filename);
  350.  
  351.   close (fd);
  352.  
  353.   /* Create and fill in the initial structure describing this file. */
  354.   defs = (DEF_FILE *)xmalloc (sizeof (DEF_FILE));
  355.   defs->filename = filename;
  356.   defs->lines = array_create (sizeof (char *));
  357.   defs->line_number = 0;
  358.   defs->production = (char *)NULL;
  359.   defs->output = (FILE *)NULL;
  360.   defs->builtins = (ARRAY *)NULL;
  361.  
  362.   /* Build the array of lines. */
  363.   i = 0;
  364.   while (i < finfo.st_size)
  365.     {
  366.       array_add (&buffer[i], defs->lines);
  367.  
  368.       while (buffer[i] != '\n' && i < finfo.st_size)
  369.     i++;
  370.       buffer[i++] = '\0';
  371.     }
  372.  
  373.   /* Begin processing the input file.  We don't write any output
  374.      until we have a file to write output to. */
  375.   output_cpp_line_info = 1;
  376.  
  377.   /* Process each line in the array. */
  378.   for (i = 0; line = defs->lines->array[i]; i++)
  379.     {
  380.       defs->line_number = i;
  381.  
  382.       if (*line == '$')
  383.     {
  384.       register int j;
  385.       char *directive;
  386.       HANDLER_ENTRY *handler;
  387.  
  388.       /* Isolate the directive. */
  389.       for (j = 0; line[j] && !whitespace (line[j]); j++);
  390.  
  391.       directive = xmalloc (j);
  392.       strncpy (directive, line + 1, j - 1);
  393.       directive[j -1] = '\0';
  394.  
  395.       /* Get the function handler and call it. */
  396.       handler = find_directive (directive);
  397.  
  398.       if (!handler)
  399.         {
  400.           line_error (defs, "Unknown directive `%s'", directive);
  401.           continue;
  402.         }
  403.       else
  404.         {
  405.           /* Advance to the first non-whitespace character. */
  406.           while (whitespace (line[j]))
  407.         j++;
  408.  
  409.           /* Call the directive handler with the FILE, and ARGS. */
  410.           (*(handler->function)) (directive, defs, line + j);
  411.         }
  412.     }
  413.       else
  414.     {
  415.       if (building_builtin)
  416.         add_documentation (defs, line);
  417.       else if (defs->output)
  418.         {
  419.           if (output_cpp_line_info)
  420.         {
  421.           fprintf (defs->output, "#line %d \"%s%s\"\n",
  422.                defs->line_number + 1,
  423.                error_directory, defs->filename);
  424.           output_cpp_line_info = 0;
  425.         }
  426.  
  427.           fprintf (defs->output, "%s\n", line);
  428.         }
  429.     }
  430.     }
  431.  
  432.   /* Close the production file. */
  433.   if (defs->output)
  434.     fclose (defs->output);
  435.  
  436.   /* The file has been processed.  Write the accumulated builtins to
  437.      the builtins.c file, and write the extern definitions to the
  438.      builtext.h file. */
  439.   write_builtins (defs, structfile, externfile);
  440.  
  441.   free (buffer);
  442.   free_defs (defs);
  443. }
  444.  
  445. #define free_safely(x) if (x) free (x)
  446.  
  447. /* Free all of the memory allocated to a DEF_FILE. */
  448. free_defs (defs)
  449.      DEF_FILE *defs;
  450. {
  451.   register int i, j;
  452.   register BUILTIN_DESC *builtin;
  453.  
  454.   if (defs->production)
  455.     free (defs->production);
  456.  
  457.   if (defs->builtins)
  458.     {
  459.       for (i = 0; builtin = (BUILTIN_DESC *)defs->builtins->array[i]; i++)
  460.     {
  461.       free_safely (builtin->name);
  462.       free_safely (builtin->function);
  463.       free_safely (builtin->shortdoc);
  464.  
  465.       if (builtin->dependencies)
  466.         {
  467.           for (j = 0; builtin->dependencies->array[j]; j++)
  468.         free (builtin->dependencies->array[j]);
  469.  
  470.           array_free (builtin->dependencies);
  471.         }
  472.  
  473.       if (builtin->longdoc)
  474.         array_free (builtin->longdoc);
  475.     }
  476.       array_free (defs->builtins);
  477.     }
  478.   free (defs);
  479. }
  480.  
  481. /* **************************************************************** */
  482. /*                                    */
  483. /*             The Handler Functions Themselves            */
  484. /*                                    */
  485. /* **************************************************************** */
  486.  
  487. /* Strip surrounding whitespace from STRING, and
  488.    return a pointer to the start of it. */
  489. char *
  490. strip_whitespace (string)
  491.      char *string;
  492. {
  493.   register int i;
  494.  
  495.   while (whitespace (*string))
  496.       string++;
  497.  
  498.   remove_trailing_whitespace (string);
  499.   return (string);
  500. }
  501.  
  502. /* Remove only the trailing whitespace from STRING. */
  503. remove_trailing_whitespace (string)
  504.      char *string;
  505. {
  506.   register int i;
  507.  
  508.   i = strlen (string) - 1;
  509.  
  510.   while (i > 0 && whitespace (string[i]))
  511.     i--;
  512.  
  513.   string[++i] = '\0';
  514. }
  515.  
  516. /* Ensure that there is a argument in STRING and return it.
  517.    FOR_WHOM is the name of the directive which needs the argument.
  518.    DEFS is the DEF_FILE in which the directive is found.
  519.    If there is no argument, produce an error. */
  520. char *
  521. get_arg (for_whom, defs, string)
  522.      char *for_whom, *string;
  523.      DEF_FILE *defs;
  524. {
  525.   char *new;
  526.  
  527.   new = strip_whitespace (string);
  528.  
  529.   if (!*new)
  530.     line_error (defs, "%s requires an argument", for_whom);
  531.  
  532.   return (savestring (new));
  533. }
  534.  
  535. /* Error if not building a builtin. */
  536. must_be_building (directive, defs)
  537.      char *directive;
  538.      DEF_FILE *defs;
  539. {
  540.   if (!building_builtin)
  541.     line_error (defs, "%s must be inside of a $BUILTIN block", directive);
  542. }
  543.  
  544. /* Return the current builtin. */
  545. BUILTIN_DESC *
  546. current_builtin (directive, defs)
  547.      char *directive;
  548.      DEF_FILE *defs;
  549. {
  550.   must_be_building (directive, defs);
  551.   return ((BUILTIN_DESC *)defs->builtins->array[defs->builtins->index - 1]);
  552. }
  553.  
  554. /* Add LINE to the long documentation for the current builtin.
  555.    Ignore blank lines until the first non-blank line has been seen. */
  556. add_documentation (defs, line)
  557.      DEF_FILE *defs;
  558.      char *line;
  559. {
  560.   register BUILTIN_DESC *builtin;
  561.   char *newline;
  562.  
  563.   builtin = current_builtin ("(implied LONGDOC)", defs);
  564.  
  565.   remove_trailing_whitespace (line);
  566.  
  567.   if (!*line && !builtin->longdoc)
  568.     return;
  569.  
  570.   if (!builtin->longdoc)
  571.     builtin->longdoc = array_create (sizeof (char *));
  572.  
  573.   array_add (line, builtin->longdoc);
  574. }
  575.  
  576. /* How to handle the $BUILTIN directive. */
  577. int
  578. builtin_handler (self, defs, arg)
  579.      char *self, *arg;
  580.      DEF_FILE *defs;
  581. {
  582.   /* If we are already building a builtin, we cannot start a new one. */
  583.   if (building_builtin)
  584.     return (line_error (defs, "%s found before $END", self));
  585.  
  586.   output_cpp_line_info++;
  587.  
  588.   /* Get the name of this builtin, and stick it in the array. */
  589.   {
  590.     BUILTIN_DESC *new;
  591.     char *name;
  592.  
  593.     name = get_arg (self, defs, arg);
  594.  
  595.     /* If this is the first builtin, create the array to hold them. */
  596.     if (!defs->builtins)
  597.       defs->builtins = array_create (sizeof (BUILTIN_DESC *));
  598.  
  599.     new = (BUILTIN_DESC *)xmalloc (sizeof (BUILTIN_DESC));
  600.     new->name = name;
  601.     new->function = (char *)NULL;
  602.     new->shortdoc = (char *)NULL;
  603.     new->longdoc = (ARRAY *)NULL;
  604.     new->dependencies = (ARRAY *)NULL;
  605.  
  606.     array_add (new, defs->builtins);
  607.     building_builtin = 1;
  608.   }
  609.   return (0);
  610. }
  611.  
  612. /* How to handle the $FUNCTION directive. */
  613. int
  614. function_handler (self, defs, arg)
  615.      char *self, *arg;
  616.      DEF_FILE *defs;
  617. {
  618.   register BUILTIN_DESC *builtin;
  619.  
  620.   builtin = current_builtin (self, defs);
  621.  
  622.   if (builtin->function)
  623.     line_error (defs, "%s already has a function (%s)",
  624.         builtin->name, builtin->function);
  625.   else
  626.     builtin->function = get_arg (self, defs, arg);
  627.  
  628.   return (0);
  629. }
  630.  
  631. /* How to handle the $SHORT_DOC directive. */
  632. short_doc_handler (self, defs, arg)
  633.      char *self, *arg;
  634.      DEF_FILE *defs;
  635. {
  636.   register BUILTIN_DESC *builtin;
  637.  
  638.   builtin = current_builtin (self, defs);
  639.  
  640.   if (builtin->shortdoc)
  641.     line_error (defs, "%s already has short documentation (%s)",
  642.         builtin->name, builtin->shortdoc);
  643.   else
  644.     builtin->shortdoc = get_arg (self, defs, arg);
  645.  
  646.   return (0);
  647. }
  648.  
  649. /* How to handle the $COMMENT directive. */
  650. comment_handler (self, defs)
  651.      char *self;
  652.      DEF_FILE *defs;
  653. {
  654. }
  655.  
  656. /* How to handle the $DEPENDS_ON directive. */
  657. depends_on_handler (self, defs, arg)
  658.      char *self, *arg;
  659.      DEF_FILE *defs;
  660. {
  661.   register BUILTIN_DESC *builtin;
  662.   char *dependent;
  663.  
  664.   builtin = current_builtin (self, defs);
  665.   dependent = get_arg (self, defs, arg);
  666.  
  667.   if (!builtin->dependencies)
  668.     builtin->dependencies = array_create (sizeof (char *));
  669.  
  670.   array_add (dependent, builtin->dependencies);
  671.  
  672.   return (0);
  673. }
  674.  
  675. /* How to handle the $PRODUCES directive. */
  676. produces_handler (self, defs, arg)
  677.      char *self, *arg;
  678.      DEF_FILE *defs;
  679. {
  680.   /* If just hacking documentation, don't change any of the production
  681.      files. */
  682.   if (only_documentation)
  683.     return (0);
  684.  
  685.   output_cpp_line_info++;
  686.  
  687.   if (defs->production)
  688.     line_error (defs, "%s already has a %s definition", defs->filename, self);
  689.   else
  690.     {
  691.       defs->production = get_arg (self, defs, arg);
  692.  
  693.       if (inhibit_production)
  694.     return (0);
  695.  
  696.       defs->output = fopen (defs->production, "w");
  697.  
  698.       if (!defs->output)
  699.     file_error (defs->production);
  700.  
  701.       fprintf (defs->output, "/* %s, created from %s. */\n",
  702.            defs->production, defs->filename);
  703.     }
  704.   return (0);
  705. }
  706.  
  707. /* How to handle the $END directive. */
  708. end_handler (self, defs, arg)
  709.      char *self, *arg;
  710.      DEF_FILE *defs;
  711. {
  712.   must_be_building (self, defs);
  713.   building_builtin = 0;
  714. }
  715.  
  716. /* **************************************************************** */
  717. /*                                    */
  718. /*            Error Handling Functions                */
  719. /*                                    */
  720. /* **************************************************************** */
  721.  
  722. /* Produce an error for DEFS with FORMAT and ARGS. */
  723. line_error (defs, format, arg1, arg2)
  724.      DEF_FILE *defs;
  725.      char *format, *arg1, *arg2;
  726. {
  727.   fprintf (stderr, "%s%s:%d:",
  728.        error_directory, defs->filename, defs->line_number + 1);
  729.   fprintf (stderr, format, arg1, arg2);
  730.   fprintf (stderr, "\n");
  731.   fflush (stderr);
  732. }
  733.  
  734. /* Print error message for FILENAME. */
  735. file_error (filename)
  736.      char *filename;
  737. {
  738.   perror (filename);
  739.   exit (2);
  740. }
  741.  
  742. /* **************************************************************** */
  743. /*                                    */
  744. /*            xmalloc and xrealloc ()                     */
  745. /*                                    */
  746. /* **************************************************************** */
  747.  
  748. static void memory_error_and_abort ();
  749.  
  750. static char *
  751. xmalloc (bytes)
  752.      int bytes;
  753. {
  754.   char *temp = (char *)malloc (bytes);
  755.  
  756.   if (!temp)
  757.     memory_error_and_abort ();
  758.   return (temp);
  759. }
  760.  
  761. static char *
  762. xrealloc (pointer, bytes)
  763.      char *pointer;
  764.      int bytes;
  765. {
  766.   char *temp;
  767.  
  768.   if (!pointer)
  769.     temp = (char *)malloc (bytes);
  770.   else
  771.     temp = (char *)realloc (pointer, bytes);
  772.  
  773.   if (!temp)
  774.     memory_error_and_abort ();
  775.  
  776.   return (temp);
  777. }
  778.  
  779. static void
  780. memory_error_and_abort ()
  781. {
  782.   fprintf (stderr, "mkbuiltins: Out of virtual memory!\n");
  783.   abort ();
  784. }
  785.  
  786. /* **************************************************************** */
  787. /*                                    */
  788. /*          Creating the Struct and Extern Files            */
  789. /*                                    */
  790. /* **************************************************************** */
  791.  
  792. /* Flags that mean something to write_documentation (). */
  793. #define QUOTED 1
  794. #define TEXINFO 2
  795.  
  796. char *structfile_header[] = {
  797.   "/* builtins.c -- the built in shell commands. */",
  798.   "",
  799.   "/* This file is manufactured by ./mkbuiltins, and should not be",
  800.   "   edited by hand.  See the source to mkbuiltins for details. */",
  801.   "",
  802.   "/* Copyright (C) 1987,1991 Free Software Foundation, Inc.",
  803.   "",
  804.   "   This file is part of GNU Bash, the Bourne Again SHell.",
  805.   "",
  806.   "   Bash is free software; you can redistribute it and/or modify it",
  807.   "   under the terms of the GNU General Public License as published by",
  808.   "   the Free Software Foundation; either version 1, or (at your option)",
  809.   "   any later version.",
  810.   "",
  811.   "   Bash is distributed in the hope that it will be useful, but WITHOUT",
  812.   "   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY",
  813.   "   or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public",
  814.   "   License for more details.",
  815.   "",
  816.   "   You should have received a copy of the GNU General Public License",
  817.   "   along with Bash; see the file COPYING.  If not, write to the Free",
  818.   "   Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */",
  819.   "",
  820.   "/* The list of shell builtins.  Each element is name, function, enabled-p,",
  821.   "   short-doc, long-doc.  The long-doc field contains a set of help",
  822.   "   lines.  The function takes a WORD_LIST *; the first word in the list",
  823.   "   is the first arg to the command.  The list has already had word.",
  824.   "   expansion performed.",
  825.   "",
  826.   "   Functions which need to look at only the simple commands (e.g.",
  827.   "   the enable_builtin ()), should ignore entries where",
  828.   "   (array[i].function == (Function *)NULL).  Such entries are for",
  829.   "   the list of shell reserved control structures, like `if' and `while'.",
  830.   "   The end of the list is denoted with a NULL name field. */",
  831.   "",
  832.   "#include \"../builtins.h\"",
  833.   (char *)NULL
  834.   };
  835.  
  836. char *structfile_footer[] = {
  837.   "  { (char *)0x0, (Function *)0x0, 0, (char *)0x0, (char *)0x0 }",
  838.   "};",
  839.   "",
  840.   "int num_shell_builtins =",
  841.   "\tsizeof (shell_builtins) / sizeof (struct builtin) - 1;",
  842.   (char *)NULL
  843. };
  844.  
  845. /* Write out any neccessary opening information for
  846.    STRUCTFILE and EXTERNFILE. */
  847. write_file_headers (structfile, externfile)
  848.      FILE *structfile, *externfile;
  849. {
  850.   register int i;
  851.  
  852.   if (structfile)
  853.     {
  854.       for (i = 0; structfile_header[i]; i++)
  855.     fprintf (structfile, "%s\n", structfile_header[i]);
  856.  
  857.       fprintf (structfile, "#include \"%s\"\n",
  858.            extern_filename ? extern_filename : "builtext.h");
  859.  
  860.       fprintf (structfile, "\nstruct builtin shell_builtins[] = {\n");
  861.     }
  862.  
  863.   if (externfile)
  864.     fprintf (externfile,
  865.          "/* %s - The list of builtins found in libbuiltins.a. */\n",
  866.          extern_filename ? extern_filename : "builtext.h");
  867. }
  868.  
  869. /* Write out any necessary closing information for
  870.    STRUCTFILE and EXTERNFILE. */
  871. write_file_footers (structfile, externfile)
  872.      FILE *structfile, *externfile;
  873. {
  874.   register int i;
  875.  
  876.   /* Write out the footers. */
  877.   if (structfile)
  878.     {
  879.       for (i = 0; structfile_footer[i]; i++)
  880.     fprintf (structfile, "%s\n", structfile_footer[i]);
  881.     }
  882. }
  883.  
  884. /* Write out the information accumulated in DEFS to
  885.    STRUCTURE_FILENAME and EXTERN_FILENAME. */
  886. write_builtins (defs, structfile, externfile)
  887.      DEF_FILE *defs;
  888.      FILE *structfile, *externfile;
  889. {
  890.   register int i;
  891.  
  892.   /* Write out the information. */
  893.   if (defs->builtins)
  894.     {
  895.       register BUILTIN_DESC *builtin;
  896.  
  897.       for (i = 0; i < defs->builtins->index; i++)
  898.     {
  899.       builtin = (BUILTIN_DESC *)defs->builtins->array[i];
  900.  
  901.       /* Write out any #ifdefs that may be there. */
  902.       if (!only_documentation)
  903.         {
  904.           if (builtin->dependencies)
  905.         {
  906.           if (builtin->function)
  907.             write_ifdefs (externfile, builtin->dependencies->array);
  908.           write_ifdefs (structfile, builtin->dependencies->array);
  909.         }
  910.  
  911.           /* Write the extern definition. */
  912.           if (builtin->function && externfile)
  913.         fprintf (externfile, "extern int %s ();\n", builtin->function);
  914.  
  915.           /* Write the structure definition. */
  916.           if (structfile)
  917.         {
  918.           fprintf (structfile, "  { \n    \"%s\", ", builtin->name);
  919.  
  920.           if (builtin->function)
  921.             fprintf (structfile, "%s, ", builtin->function);
  922.           else
  923.             fprintf (structfile, "(Function *)0x0, ");
  924.  
  925.           fprintf
  926.             (structfile, "1, \"%s\",\n",
  927.              builtin->shortdoc ? builtin->shortdoc : builtin->name);
  928.  
  929.           write_documentation
  930.             (structfile, builtin->longdoc->array, 4, QUOTED);
  931.       
  932.           fprintf (structfile, "  },\n");
  933.         }
  934.  
  935.           /* Write out the matching #endif, if neccessary. */
  936.           if (builtin->dependencies)
  937.         {
  938.           if (builtin->function)
  939.             write_endifs (externfile, builtin->dependencies->array);
  940.           write_endifs (structfile, builtin->dependencies->array);
  941.         }
  942.         }
  943.  
  944.       if (documentation_file)
  945.         {
  946.           fprintf (documentation_file, "@item %s\n", builtin->name);
  947.           write_documentation
  948.         (documentation_file, builtin->longdoc->array, 0, TEXINFO);
  949.         }
  950.     }
  951.     }
  952. }
  953.  
  954. /* Write an #ifdef string saying what needs to be defined (or not defined)
  955.    in order to allow compilation of the code that will follow.
  956.    STREAM is the stream to write the information to,
  957.    DEFINES is a null terminated array of define names.
  958.    If a define is preceded by an `!', then the sense of the test is
  959.    reversed. */
  960. write_ifdefs (stream, defines)
  961.      FILE *stream;
  962.      char **defines;
  963. {
  964.   register int i;
  965.  
  966.   if (!stream)
  967.     return;
  968.  
  969.   fprintf (stream, "#if ");
  970.  
  971.   for (i = 0; defines[i]; i++)
  972.     {
  973.       char *def = defines[i];
  974.  
  975.       if (*def == '!')
  976.     fprintf (stream, "!defined (%s)", def + 1);
  977.       else
  978.     fprintf (stream, "defined (%s)", def);
  979.  
  980.       if (defines[i + 1])
  981.     fprintf (stream, " && ");
  982.     }
  983.   fprintf (stream, "\n");
  984. }
  985.  
  986. /* Write an #endif string saying what defines controlled the compilation
  987.    of the immediately preceding code.
  988.    STREAM is the stream to write the information to.
  989.    DEFINES is a null terminated array of define names. */
  990. write_endifs (stream, defines)
  991.      FILE *stream;
  992.      char **defines;
  993. {
  994.   register int i;
  995.  
  996.   if (!stream)
  997.     return;
  998.  
  999.   fprintf (stream, "#endif /* ");
  1000.  
  1001.   for (i = 0; defines[i]; i++)
  1002.     {
  1003.       fprintf (stream, "%s", defines[i]);
  1004.  
  1005.       if (defines[i + 1])
  1006.     fprintf (stream, " && ");
  1007.     }
  1008.  
  1009.   fprintf (stream, " */\n");
  1010. }
  1011.  
  1012. /* Write DOCUMENTAION to STREAM, perhaps surrounding it with double-quotes
  1013.    and quoting special characters in the string. */
  1014. write_documentation (stream, documentation, indentation, flags)
  1015.      FILE *stream;
  1016.      char **documentation;
  1017.      int indentation, flags;
  1018. {
  1019.   register int i, j;
  1020.   register char *line;
  1021.   int quoted = (flags & QUOTED); /* Mutually exclusive. */
  1022.   int texinfo = (flags & TEXINFO);
  1023.  
  1024.   if (!stream)
  1025.     return;
  1026.  
  1027.   if (quoted)
  1028.     {
  1029.       if (indentation)
  1030.     fprintf (stream, "\"\\\n");
  1031.       else
  1032.     fprintf (stream, "\"");
  1033.     }
  1034.  
  1035.   for (i = 0; line = documentation[i]; i++)
  1036.     {
  1037.       if (indentation)
  1038.     for (j = 0; j < indentation; j++)
  1039.       fprintf (stream, " ");
  1040.  
  1041.       if (quoted)
  1042.     {
  1043.       for (j = 0; line[j]; j++)
  1044.         {
  1045.           switch (line[j])
  1046.         {
  1047.         case '\\':
  1048.         case '"':
  1049.           fprintf (stream, "\\%c", line[j]);
  1050.           break;
  1051.  
  1052.         default:
  1053.           fprintf (stream, "%c", line[j]);
  1054.         }
  1055.         }
  1056.  
  1057.       if (documentation[i + 1])
  1058.         fprintf (stream, "\\n\\\n");
  1059.     }
  1060.       else if (texinfo)
  1061.     {
  1062.       for (j = 0; line[j]; j++)
  1063.         {
  1064.           switch (line[j])
  1065.         {
  1066.         case '@':
  1067.         case '{':
  1068.         case '}':
  1069.           fprintf (stream, "@%c", line[j]);
  1070.           break;
  1071.  
  1072.         default:
  1073.           fprintf (stream, "%c", line[j]);
  1074.         }
  1075.         }
  1076.       fprintf (stream, "\n");
  1077.     }
  1078.       else
  1079.     fprintf (stream, "%s\n", line);
  1080.     }
  1081.  
  1082.   if (quoted)
  1083.     fprintf (stream, "\"\n");
  1084. }
  1085.